<template>
  <el-dialog
    append-to-body
    :title="title"
    :visible.sync="dialogFormVisible"
    width="500px"
    @close="close"
  >
    <el-form ref="form" label-width="80px" :model="form" :rules="rules">
      <el-form-item label="系统账号" prop="username">
        <el-input v-model.trim="form.username" />
      </el-form-item>
      <el-form-item label="系统密码" prop="mpassword">
        <el-input v-model.trim="form.mpassword" show-password />
      </el-form-item>
      <el-form-item label="员工昵称" prop="nickname">
        <el-input v-model.trim="form.nickname" />
      </el-form-item>
      <el-form-item label="员工姓名" prop="realname">
        <el-input v-model.trim="form.realname" />
      </el-form-item>
      <el-form-item label="联系方式" prop="phone">
        <el-input v-model.trim="form.phone" />
      </el-form-item>
      <el-form-item v-if="isHidden" label="所属门店" prop="storeid">
        <el-select
          v-model="form.storeid"
          multiple
          placeholder="请选择门店"
          style="width: 100%"
        >
          <el-option
            v-for="item in options"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="角色权限" prop="roleid">
        <el-select
          v-model="form.roleid"
          placeholder="请选择角色"
          style="width: 100%"
        >
          <el-option
            v-for="item in roptions"
            :key="item.id"
            :label="item.role_name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="账号状态" prop="status">
        <el-select v-model="form.status" style="width: 100%">
          <el-option label="启用" value="Y" />
          <el-option label="禁用" value="N" />
        </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </template>
  </el-dialog>
</template>

<script>
  import { getStoreList } from '@/api/staff'
  import { doEdit } from '@/api/user'
  import { getRoleList } from '@/api/role'
  export default {
    name: 'UserEdit',
    data() {
      return {
        options: [],
        roptions: [],
        isHidden: true,
        form: {
          id: '',
          username: '',
          mpassword: '',
          storeid: [],
          roleid: '',
          is_super: 'N',
          status: 'Y',
        },
        rules: {
          username: [
            { required: true, trigger: 'blur', message: '请输入账号' },
          ],
          mpassword: [
            { required: true, trigger: 'blur', message: '请输入密码' },
          ],
          nickname: [
            { required: true, trigger: 'blur', message: '请输入昵称' },
          ],
          realname: [
            { required: true, trigger: 'blur', message: '请输入姓名' },
          ],
          phone: [
            { required: true, trigger: 'blur', message: '请输入联系方式' },
          ],
          storeid: [
            { required: true, trigger: 'change', message: '请选择门店' },
          ],
          roleid: [
            { required: true, trigger: 'change', message: '请选择角色' },
          ],
          is_super: [
            {
              required: true,
              trigger: 'change',
              message: '请选择是否为超级管理员',
            },
          ],
          status: [
            { required: true, trigger: 'change', message: '请选择状态' },
          ],
        },
        title: '',
        dialogFormVisible: false,
      }
    },
    created() {
      this.fetchData()
      this.roleData()
    },
    methods: {
      showEdit(row) {
        if (!row) {
          this.title = '添加'
        } else {
          this.title = '编辑'
          this.form = Object.assign({}, row)
          if (row.is_super === 'Y') {
            this.isHidden = false
          } else {
            this.isHidden = true
          }
          console.log(this.form)
        }
        this.dialogFormVisible = true
      },
      handleChange(value) {
        if (value === 'Y') {
          this.isHidden = false
        } else {
          this.isHidden = true
        }
      },
      close() {
        this.$refs['form'].resetFields()
        this.form = this.$options.data().form
        this.dialogFormVisible = false
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          console.log(this.form)

          if (valid) {
            const { msg } = await doEdit(this.form)
            this.$baseMessage(msg, 'success', 'vab-hey-message-success')
            this.$emit('fetch-data')
            this.close()
          }
        })
      },

      async fetchData() {
        const data = await getStoreList()
        this.options = data.data
      },

      async roleData() {
        const data = await getRoleList()
        this.roptions = data.data
      },
    },
  }
</script>
